package org.teamh.gui.ingame;

import java.util.HashMap;

import org.teamh.logic.datamodel.Cell;
import org.teamh.logic.datamodel.DatamodelManager;
import org.teamh.logic.datamodel.Waypoint;

public class LoadWorld {
	private DatamodelManager model;
	private static final int ROTATE = 45;

	public LoadWorld(DatamodelManager model) {
		this.model = model;
		init();
	}

	private void init() {
		countingWorld();
		rotateCreep();
	}

	public void countingWorld() {
		float worldX = 0;
		float worldY = 0;
		int modelX;
		int modelY;
		HashMap<String, Cell> cells = model.getCells();
		for (Cell cell : cells.values()) {
			modelX = cell.getX();
			modelY = cell.getY();

			if (cell.getMap().getTile().getPosition().contains("TOP_LEFT")) {
				worldX = ((modelX - 16) * (1 / 16f)) + (1 / 32f);
				worldY = -((16 - modelY) * (1 / 16f)) + (1 / 32f);
			}
			if (cell.getMap().getTile().getPosition().contains("TOP_RIGHT")) {
				worldX = ((modelX) * (1 / 16f)) + (1 / 32f);
				worldY = -((16 - modelY) * (1 / 16f)) + (1 / 32f);
			}
			if (cell.getMap().getTile().getPosition().contains("BOTTOM_LEFT")) {
				worldX = ((modelX - 16) * (1 / 16f)) + (1 / 32f);
				worldY = ((modelY) * (1 / 16f)) + (1 / 32f);
			}
			if (cell.getMap().getTile().getPosition().contains("BOTTOM_RIGHT")) {
				worldX = ((modelX) * (1 / 16f)) + (1 / 32f);
				worldY = ((modelY) * (1 / 16f)) + (1 / 32f);
			}
			cell.setWorldX(worldX);
			cell.setWorldY(worldY);
		}
	}

	public void rotateCreep() {
		float current_x = 0;
		float current_y = 0;
		float next_x = 0;
		float next_y = 0;
		HashMap<String, Waypoint> points = model.getWaypoints();
		for (Waypoint point : points.values()) {
			current_x = point.getCell().getWorldX();
			current_y = point.getCell().getWorldY();
			try {
				next_x = point.getNext().getCell().getWorldX();
				next_y = point.getNext().getCell().getWorldY();

				float distance = (float)Math.sqrt(Math.pow(next_x - current_x, 2) + Math.pow(next_y - current_y, 2));
//				float distance = Math.abs(next_x - current_x) + Math.abs(next_y - current_y);
				
				point.setWayToPoint(distance);
				
				if (current_x > next_x && current_y == next_y) {
					point.setRotateUnit(2 * ROTATE);
					continue;
				}
				if (current_x > next_x && current_y < next_y) {
					point.setRotateUnit(3 * ROTATE);
					continue;
				}
				if (current_x == next_x && current_y < next_y) {
					point.setRotateUnit(4 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y < next_y) {
					point.setRotateUnit(5 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y == next_y) {
					point.setRotateUnit(6 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y > next_y) {
					point.setRotateUnit(7 * ROTATE);
					continue;
				}
				if (current_x == next_x && current_y > next_y) {
					point.setRotateUnit(0);
					continue;
				}
				if (current_x > next_x && current_y > next_y) {
					point.setRotateUnit(ROTATE);
					continue;
				}
			} catch (Exception e) {

			}
		}
	}

}
